//
//  UIButton+SWAdd.m
//  EnjoyEasy
//
//  Created by 曾少伟 on 2016/12/3.
//  Copyright © 2016年 腾程－zsw. All rights reserved.
//

#import "UIButton+SWAdd.h"
static UIImageView *_goldImgView = nil;
@implementation UIButton (SWAdd)
+ (instancetype)buttonWithTitle:(NSString *)title
                     titleColor:(UIColor *)titleColor
                           font:(CGFloat)font
                backgroundColor:(UIColor *)backgroundColor
                         tagart:(id)target
                         action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:font];
    button.backgroundColor = backgroundColor;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (instancetype)buttonWithTitle:(NSString *)title
                     titleColor:(UIColor *)titleColor
                           font:(CGFloat)font
                      imageName:(NSString *)imageName
                         tagart:(id)target
                         action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:font];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:[imageName stringByAppendingString:@"_selected"]] forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (instancetype)buttonWithTitle:(NSString *)title
                     titleColor:(UIColor *)titleColor
                           font:(CGFloat)font
            backgroundImageName:(NSString *)backgroundImageName
                         tagart:(id)target
                         action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:font];
    [button setBackgroundImage:[UIImage imageNamed:backgroundImageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:[backgroundImageName stringByAppendingString:@"_selected"]] forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (instancetype)buttonWithBackgroundImageName:(NSString *)backgroundImageName
                                       tagart:(id)target
                                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:backgroundImageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:[backgroundImageName stringByAppendingString:@"_selected"]] forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (instancetype)buttonWithBackgroundImage:(UIImage *)backgroundImage
                                   tagart:(id)target
                                   action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (instancetype)buttonWithBackgroundImage:(UIImage *)backgroundImage
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    return button;
}

- (void)setImageEdgeInsetsWithOffestX:(CGFloat)offsetX YOffsetY:(CGFloat)offsetY {
    self.imageEdgeInsets = UIEdgeInsetsMake(
            self.currentBackgroundImage.size.height/4 - self.currentImage.size.height/4 + offsetY,
           self.currentBackgroundImage.size.width/4 - self.currentImage.size.width/4 + offsetX,
           self.currentBackgroundImage.size.height/4 - self.currentImage.size.height/4 - offsetY,
           self.currentBackgroundImage.size.width/4 - self.currentImage.size.width/4 - offsetX);
}

- (void)setContentImageWithPaiStr:(NSString *)paiStr
                          offestX:(CGFloat)offsetX
                          offsetY:(CGFloat)offsetY
                         imgScale:(CGFloat)scale
                  isConverToBigMj:(BOOL)isConverToBigMj
{
//    //显示金牌
//    if ([paiStr isEqualToString:@"0"]) {
//        paiStr = [PaiModel sharePaiModel].goldPai;
//    }
//    NSString *imgName = isConverToBigMj?[Helper convertPaiToBigMJNameWithPaiStr:paiStr]:[Helper convertPaiToSmallMJNameWithPaiStr:paiStr];
//    UIImage *contentImg = [UIImage cutimageFromSourceName:QZGAMEMJ andPlistImageName:imgName
//                                          imgScale:scale];
//    UIImage * bgImg = [UIImage imageWithColor:[UIColor clearColor] size:self.currentBackgroundImage.size];
//    bgImg = [bgImg drawImageOnCenter:contentImg];
//    if ([paiStr isEqualToString:[PaiModel sharePaiModel].goldPai]) {
//        UIImage *goldImg = isConverToBigMj?[UIImage cutimageFromSourceName:QZGAMEBASE andPlistImageName:@"QZMJ_card_gold_b.png"imgScale:0.7]:[UIImage cutimageFromSourceName:QZGAMEBASE andPlistImageName:@"QZMJ_card_gold_m.png"imgScale:0.7];
//        bgImg = [bgImg appendGoldImg:goldImg offsetX:offsetX];
//    }
//    [self setImage:bgImg forState:UIControlStateNormal];
//    self.imageEdgeInsets = UIEdgeInsetsMake(offsetY,offsetX,-offsetY,-offsetX);
//    [self setImageEdgeInsetsWithOffestX:offsetX YOffsetY:offsetY];
}

- (void)setContentImageWithPaiStr:(NSString *)paiStr
                          offestX:(CGFloat)offsetX
                          offsetY:(CGFloat)offsetY
                         imgScale:(CGFloat)scale
                  isConverToBigMj:(BOOL)isConverToBigMj
                           rotate:(ImageRotateDirection)rotate
{
//    //显示金牌
//    if ([paiStr isEqualToString:@"0"]) {
//        paiStr = [PaiModel sharePaiModel].goldPai;
//    }
//    NSString *imgName = isConverToBigMj?[Helper convertPaiToBigMJNameWithPaiStr:paiStr]:[Helper convertPaiToSmallMJNameWithPaiStr:paiStr];
//    
//    UIImage *contentImg = [UIImage cutimageFromSourceName:QZGAMEMJ andPlistImageName:imgName
//                                          imgScale:scale];
//   
//    UIImage * bgImg = [UIImage imageWithColor:[UIColor clearColor] size:self.currentBackgroundImage.size];
//    bgImg = [bgImg drawImageOnCenter:contentImg];
//    if ([paiStr isEqualToString:[PaiModel sharePaiModel].goldPai]) {
//        UIImage *goldImg = isConverToBigMj?[UIImage cutimageFromSourceName:QZGAMEBASE andPlistImageName:@"QZMJ_card_gold_b.png"imgScale:0.7]:[UIImage cutimageFromSourceName:QZGAMEBASE andPlistImageName:@"QZMJ_card_gold_m.png"imgScale:0.7];
//        bgImg = [bgImg appendGoldImg:goldImg offsetX:-offsetY];
//    }
//    switch (rotate) {
//        case ImageRotateDirectionLeft90:
//            bgImg = [bgImg imageByRotateLeft90];
//            break;
//        case ImageRotateDirectionRight90:
//            bgImg = [bgImg imageByRotateRight90];
//            break;
//        default:
//            break;
//    }
//    [self setImage:bgImg forState:UIControlStateNormal];
//    self.imageEdgeInsets = UIEdgeInsetsMake(offsetY,offsetX,- offsetY,- offsetX);
//    [self setImageEdgeInsetsWithOffestX:offsetX YOffsetY:offsetY];
}

- (void)setImageWithPlist:(NSString *)plist imgName:(NSString *)imageName {
    UIImage *bgImg = [UIImage cutimageFromSourceName:plist andPlistImageName:imageName];
    [self setImage:bgImg forState:UIControlStateNormal];
    self.imageEdgeInsets = UIEdgeInsetsMake(
        self.currentBackgroundImage.size.height/4 - self.currentImage.size.height/4 -2,
        self.currentBackgroundImage.size.width/4 - self.currentImage.size.width/4,
        self.currentBackgroundImage.size.height/4 - self.currentImage.size.height/4+2,
        self.currentBackgroundImage.size.width/4 - self.currentImage.size.width/4);
}

- (void)setBackgroundWithPlist:(NSString *)plist imgName:(NSString *)imageName {
//    UIImage *img = [UIImage cutimageFromSourceName:plist andPlistImageName:imageName];
//    [self setBackgroundImage:img forState:UIControlStateNormal];
//    NSAssert(self.superview, @"请先添加父视图");
//    [self mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(img.size.width/2);
//        make.height.mas_equalTo(img.size.height/2);
//    }];
}

- (void)setContentImage:(UIImage *)image
//        imgScale:(CGFloat)scale
                offestX:(CGFloat)offsetX
                offsetY:(CGFloat)offsetY
                rotate:(ImageRotateDirection)rotate
{
    switch (rotate) {
        case ImageRotateDirectionLeft90:
            image = [image imageByRotateLeft90];
            break;
        case ImageRotateDirectionRight90:
            image = [image imageByRotateRight90];
            break;
        default:
            break;
    }
    [self setImage:image forState:UIControlStateNormal];
    self.imageEdgeInsets = UIEdgeInsetsMake(offsetY,offsetX,- offsetY,- offsetX);
//    [self setImageEdgeInsetsWithOffestX:offsetX YOffsetY:offsetY];
}

//- (UIImageView *)goldImgView {
//    if (_goldImgView == nil) {
//        _goldImgView = [[UIImageView alloc]init];
//        UIImage *goldImg = [UIImage cutimageFromSourceName:QZGAMEBASE andPlistImageName:@"QZMJ_card_gold_m.png"imgScale:0.7];
//        _goldImgView.image = goldImg;
//        _goldImgView.backgroundColor = BgColor;
//        [self addSubview:_goldImgView];
//        [_goldImgView mas_makeConstraints:^(MASConstraintMaker *make) {
////            make.top.equalTo(self).offset(2);
////            make.right.equalTo(self).offset(-2);
////            make.width.mas_equalTo(goldImg.size.width/2);
////            make.height.mas_equalTo(goldImg.size.height/2);
//            make.edges.equalTo(self).insets(UIEdgeInsetsMake(0,0,0,0));
//        }];
//    }
//    return _goldImgView;
//}
//
//- (void)setGoldImgView:(UIImageView *)goldImgView {
//    _goldImgView = goldImgView;
//}

@end
